package book.controller;

import book.entity.Dz_ShenFen;
import book.entity.IdsModel;
import book.entity.duzhe;
import book.entity.tushu;
import book.service.DuzheServices;
import book.service.Dz_ShenFenServices;
import book.service.UserServices;
import book.service.impl.DuZheServiceImpl;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/duzhe")
public class DuzheContrller {

    @Autowired
    DuzheServices duzheServices;

    @Autowired
    Dz_ShenFenServices dz_shenFen;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        List<Dz_ShenFen> shenfen = dz_shenFen.getAll();
        request.setAttribute("shenfen",shenfen);
        return "user/user";
    }

    @RequestMapping("/chongzhi")
    public String chongzhi(HttpServletRequest request){
        List<Dz_ShenFen> shenfen = dz_shenFen.getAll();
        request.setAttribute("aa",shenfen);
        return "user/chongzhi";
    }

//    @RequestMapping(value = "/getById",method = RequestMethod.POST)
//    @ResponseBody
//    public duzhe getById(Integer id){
//        System.err.println(id);
//        duzhe list=duzheServices.getById(id);
//        return list;
//    }

    @RequestMapping(value = "/all",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cha(@RequestParam("offset") int offset,@RequestParam("limit") int limit,duzhe condition){
        List<duzhe> list=duzheServices.getAll(offset,limit,condition);
        Map<String, Object> map = new HashMap<String,Object>();
        int total = duzheServices.getCount();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toadd(HttpServletRequest request){
        List<Dz_ShenFen> list=dz_shenFen.getAll();
        request.setAttribute("ShenFen",list);
        return "user/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(duzhe dz){
        System.out.println(dz.getDz_shenFen().getSf_id());
        Map<String, Object> map = new HashMap<String,Object>();
        duzheServices.insert(dz);
        map.put("result",true);
        return map;
    }



    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("id") int id){
        Map<String, Object> map = new HashMap<String,Object>();
        duzheServices.delete(id);
        map.put("result",true);
        return map;
    }


    @RequestMapping(value = "/batchDel",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> todelete(IdsModel idsModel){
        Map<String, Object> map = new HashMap<String,Object>();
        System.out.println(idsModel.getIds() != null);
        duzheServices.todel(idsModel.getIds());
        map.put("result",true);
        return map;
    }


    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public String update(@RequestParam("id") int id,HttpServletRequest request){
        duzhe d=duzheServices.getById(id);
        List<Dz_ShenFen> shenfen = dz_shenFen.getAll();
        request.setAttribute("duixiang",d);
        request.setAttribute("list",shenfen);
        return "user/update";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> toupdate(duzhe dz){
        Map<String, Object> map = new HashMap<String,Object>();
        duzheServices.update(dz);
        map.put("result",true);
        return map;
    }

    @RequestMapping(value = "/chongzhi",method = RequestMethod.GET)
    public String chongzhi(@RequestParam("id") int id,HttpServletRequest request){
        duzhe d=duzheServices.getById(id);
        List<Dz_ShenFen> shenfen = dz_shenFen.getAll();
        request.setAttribute("duixiang",d);
        request.setAttribute("list",shenfen);
        return "user/chongzhi";
    }

    @RequestMapping(value = "/chongzhi",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> tochongzhi(duzhe dz){
        System.err.println(JSON.toJSONString(dz));
        Map<String, Object> map = new HashMap<String,Object>();
        duzheServices.chongzhi(dz);
        map.put("result",true);
        return map;
    }
}
